<?php
include_once('connection.functions.php');


function debug_query($sql, $db = True)
{
	$query = query($sql,null,$db);
	if($query['error'])
	{
		echo "<div class='ui-widget'>
			<div class='ui-state-error ui-corner-all' style='padding: 0 .7em;'> 
				<p><span class='ui-icon ui-icon-alert' style='float: left; margin-right: .3em;'></span> 
				<strong>Error:</strong> ".$query['error']."</p>
			</div>
                   </div>  ";
	}else{
			
		echo "<div class='ui-widget'>
			<div class='ui-state-highlight ui-corner-all' style='margin-top: 20px; padding: 0 .7em;'> 
				<p><span class='ui-icon ui-icon-info' style='float: left; margin-right: .3em;'></span>
				<strong>Hey!</strong> The query: ".$sql." has been executed correctly.</p>
			</div>
		</div>";
			
			
	}

}

function get_biobit($nickname)
{
	$query= query("SELECT * FROM biobit WHERE bb_name = ? LIMIT 1", array($nickname));
	if($query['error'])
	{
		header("Location: error.php?err=".$query['error']);
		return -9;
	}else{

		return $query;
	}
}

function get_all_biobits()
{
	$query= query("SELECT * FROM biobit");

	if($query['error'])
	{
		header("Location: error.php?err=".$query['error']);
		return -9;
	}else{

		return $query;
	}
}

function add_biobit( $biobit_name, $biobit_desc, $biobit_link)
{

	$query= query("INSERT INTO `biobit` (`bb_name`, `bb_desc`, `bb_link`) VALUES (?, ?, ?)", array($biobit_name, $biobit_desc, $biobit_link));

	if($query['error'])
	{
		header("Location: error.php?err=".$query['error']);
		return -9;
	}else{

		return $query;
	}
}
function get_all_hws($biobit)
{
	$query= query("SELECT * FROM hws WHERE bb_name=?", array($biobit));

	if($query['error'])
	{
		header("Location: error.php?err=".$query['error']);
		return -9;
	}else{

		return $query;
	}
}



//SEMICABLES
function get_all_hw(){
	$query= query("SELECT * FROM hws");
	if($query['error'])
	{
		header("Location: error.php?err=".$query['error']);
		return -9;
	}else{

		return $query;
	}
}
function get_hws($nickname)
{
	$query= query("SELECT * FROM hws WHERE hws_name = ? LIMIT 1", array($nickname));
	if($query['error'])
	{
		header("Location: error.php?err=".$query['error']);
		return -9;
	}else{

		return $query;
	}
}

function add_hws($name, $bb_name,$comments, $hws_link, $testBy, $type)
{
	$query = query("INSERT INTO `hws` (`hws_name`, `bb_name`, `hws_comments`,`hws_linkBB`,`hws_testBy`,`hws_type` )
VALUES (?, ?, ?,?,?,?)", array($name, $bb_name,$comments, $hws_link, $testBy, $type));

	if($query['error'])
	{
		header("Location: error.php?err=".$query['error']);
		return -9; //TODO: poner en los que faltan
	}else{

		return $query;
	}
}

function add_targProm($targInd, $targInd_species, $targInd_fasta)
{
	$query = query("INSERT INTO `targProm` (`tp_name`, `tp_species`, `tp_Fasta`)
VALUES (?, ?, ?)", array($targInd, $targInd_species, $targInd_fasta));

	if($query['error'])
	{
		header("Location: error.php?err=".$query['error']);
		return -9;
	}else{

		return $query;
	}
}

function add_TP_hws($name,$targInd)
{
	$query = query("INSERT INTO `tpHws` (`hws_name`, `tp_name` )
VALUES (?, ?)", array($name,$targInd));

	if($query['error'])
	{
		header("Location: error.php?err=".$query['error']);
		return -9;
	}else{

		return $query;
	}
}

function get_tp_from_rhw($hws)
{
	$query= query("SELECT TP.tp_name,tp_species, tp_Fasta FROM tpHws TPH, targProm TP WHERE TPH.tp_name=TP.tp_name AND TPH.hws_name=?", array($hws));
	if($query['error'])
	{
		header("Location: error.php?err=".$query['error']);
		return -9;
	}else{

		return $query;
	}
}

//GENES
function get_all_genes(){
	$query= query("SELECT * FROM gen");
	if($query['error'])
	{
		header("Location: error.php?err=".$query['error']);
		return -9;
	}else{

		return $query;
	}
}
function get_gene($nickname)
{
	$query= query("SELECT * FROM gen WHERE g_name = ? LIMIT 1", array($nickname));
	if($query['error'])
	{
		header("Location: error.php?err=".$query['error']);
		return -9;
	}else{

		return $query;
	}
}

function get_genes_from_hws($hws)
{
	$query= query("SELECT G.g_name,g_species,g_codSus,g_linkBB, g_linkBD, g_Fasta FROM gen G, genHws GH WHERE GH.hws_name=? AND G.g_name = GH.g_name", array($hws));
	if($query['error'])
	{
		header("Location: error.php?err=".$query['error']);
		return -9;
	}else{

		return $query;
	}
}

function add_gen( $gen_name, $gen_spice, $gen_codSus,$gen_linkbb,$gen_linkbd,$gen_fasta)
{

	$query= query("INSERT INTO `gen` (`g_name`, `g_species`, `g_species`, `g_codSus`, `g_linkBB`, `g_linkBD`, `g_Fasta`) VALUES (?, ?, ?, ?, ?, ?)", array($gen_name, $gen_spice, $gen_codSus,$gen_linkbb,$gen_linkbd,$gen_fasta));

	if($query['error'])
	{
		header("Location: error.php?err=".$query['error']);
		return -9;
	}else{

		return $query;
	}
}

function add_gen_hws( $hws_name,$gen_name )
{

	$query= query("INSERT INTO `genHws` (`hws_name`, `g_name` ) VALUES (?, ?)", array( $gen_name, $hws_name));

	if($query['error'])
	{
		header("Location: error.php?err=".$query['error']);
		return -9;
	}else{

		return $query;
	}
}



?>
